package com.qc.ayouth.shop.admin.controller.goods;

import com.alibaba.fastjson.JSONObject;
import com.qc.ayouth.shop.admin.beans.AdminResponseVO;
import com.qc.ayouth.shop.admin.beans.dto.CategoryDto;
import com.qc.ayouth.shop.admin.beans.dto.CategorySimpleDto;
import com.qc.ayouth.shop.admin.service.shop.AdminCategoryService;
import com.qc.ayouth.shop.core.beans.bo.ServiceResultBO;
import com.qc.ayouth.shop.core.beans.bo.ValidResultBO;
import com.qc.ayouth.shop.core.constants.ServiceConstants;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 用户相关
 * Created by Haojg on 2020/7/5
 */
@RestController
@RequestMapping("/api/admin/category")
@Validated
@Slf4j
public class AdminCategoryController {

    @Autowired
    AdminCategoryService adminCategoryService;

    @RequiresPermissions("category:list")
    @GetMapping("/list")
    public AdminResponseVO list() {
        log.info("admin category list start");
        AdminResponseVO vo = new AdminResponseVO();
        ServiceResultBO bo = adminCategoryService.getGoodsCategoryList();
        if (bo.getCode() == ServiceConstants.SERVICE_RESULT_FAIL) {
            vo.fail(bo.getMsg());
            return vo;
        }
        vo.setData(bo.getData());
        vo.ok();
//        List<CategoryDto> catList = adminCategoryService.getCategoryList();
//        vo.add("categoryList", catList).ok();
        log.info("admin category list end");
        return vo;
    }

    @RequiresPermissions("category:list")
    @GetMapping("/getMainCatList")
    public AdminResponseVO getMainCatList() {
        log.info("admin listMainCat start");
        AdminResponseVO vo = new AdminResponseVO();
        List<CategorySimpleDto> catList = adminCategoryService.getMainCategoryList();
        vo.add("catList", catList).ok();
        log.info("admin listMainCat end");
        return vo;
    }

    @RequiresPermissions("category:list")
    @GetMapping("/getCatChildrenList")
    public AdminResponseVO getCatChildrenList(Long catId) {
        log.info("admin getCatChildrenList start");
        AdminResponseVO vo = new AdminResponseVO();
        List<CategoryDto> catList = adminCategoryService.getCategoryChildrenList(catId);
        vo.add("catList", catList).ok();
        log.info("admin getCatChildrenList end");
        return vo;
    }

    @RequiresPermissions("category:list")
    @PostMapping("/create")
    public AdminResponseVO create(@RequestBody String jsonParam) {
        log.info("admin category create start");
        AdminResponseVO vo = new AdminResponseVO();
        JSONObject param = JSONObject.parseObject(jsonParam);
        // 1.check 参数
        ValidResultBO validBo = adminCategoryService.validCreateCategory(param);
        if (validBo.getStatus() == ServiceConstants.SERVICE_VALID_FAIL) {
            vo.fail(validBo.getMsg());
            return vo;
        }

        // 2.保存数据
        if (!adminCategoryService.saveCategory(param)) {
            vo.saveDataFailed();
            return vo;
        }
        vo.ok();
        log.info("admin category create end");
        return vo;
    }

    @RequiresPermissions("category:list")
    @PostMapping("/update")
    public AdminResponseVO update(@RequestBody String jsonParam) {
        log.info("admin category update start");
        AdminResponseVO vo = new AdminResponseVO();
        JSONObject param = JSONObject.parseObject(jsonParam);
        // 1.check 参数
        ValidResultBO validBo = adminCategoryService.validCreateCategory(param);
        if (validBo.getStatus() == ServiceConstants.SERVICE_VALID_FAIL) {
            vo.fail(validBo.getMsg());
            return vo;
        }

        // 2.更新数据
        if (!adminCategoryService.updateCategory(param)) {
            vo.updatedDataFailed();
            return vo;
        }
        vo.ok();
        log.info("admin category update end");
        return vo;
    }

    @RequiresPermissions("category:list")
    @PostMapping("/delete")
    public AdminResponseVO delete(@RequestBody String jsonParam) {
        log.info("admin category delete start");
        AdminResponseVO vo = new AdminResponseVO();
        JSONObject param = JSONObject.parseObject(jsonParam);
        // 1.check 参数
        if (StringUtils.isEmpty(param.getString("id"))) {
            vo.badArgument();
            return vo;
        }

        // 2.删除数据
        if (!adminCategoryService.deleteCategory(param.getLong("id"))) {
            vo.deleteDataFailed();
            return vo;
        }

        vo.ok();
        log.info("admin category delete end");
        return vo;
    }



}
